package com.mmp.main.servlet.joborder;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.endpoints.JobOrderNotificationsEndPoint;
import com.mmp.main.endpoints.SPInventoryNotificationsEndPoint;
import com.mmp.main.entity.*;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class JobOrderTrackingProcessServlet extends BasicServlet {

	@Autowired
	JobOrderNotificationsEndPoint joNotifier;

	@Autowired
	SPInventoryNotificationsEndPoint spCategoryNotifier;

	protected void updateNotifications(HttpServletRequest request) {
		int siteId = getSessionSiteId(request);
		joNotifier.broadcastNotificationUpdates(siteId);
		spCategoryNotifier.broadcastNotificationUpdates(siteId);
	}

	protected void updateNotificationsThenGoForward(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		updateNotifications(request);

		Common.log("submit btn name : " + request.getParameter(Default.SAVE_THEN_RETURN_BTN_NAME));
		Common.log("back btn name : " + request.getParameter(Default.SAVE_THEN_GO_BACK_BTN_NAME));

		if (WebUtil.getParamValue(request, Default.SAVE_THEN_RETURN_BTN_NAME, null) != null){
			if(request.getParameter("showJobOrderReport") != null)
				sendRedirect(request, response, "ViewJobOrderReport?jobOrderNo=" + request.getParameter("jobOrderNo"));
			else
				sendRedirect(request, response, "FollowupJobOrderSearch");
		}
		else if (WebUtil.getParamValue(request, Default.SAVE_THEN_CONTINUE_BTN_NAME, null) != null)
			basicForward(request, response, "FollowupJobOrder");

		else if (WebUtil.getParamValue(request, Default.SAVE_THEN_GO_BACK_BTN_NAME, null) != null) {
			String uri = WebUtil.getRequestUri(request);
			String reqPhaseId = null;
			if ("JobOrderFollowupSecondPhaseProcess".equals(uri))
				reqPhaseId = "" + Enums.JOBORDER_FOLLOWUP_PHASES.FIRST_ACTION_TAKEN.getId();
			// else if ("JobOrderFollowupThirdPhaseProcess".equals(uri))
			// reqPhaseId = "" +
			// Enums.JOBORDER_FOLLOWUP_PHASES.SECOND_ACTION_TAKEN.getId();

			request.setAttribute(Default.JOBORDER_PHASE_ATTR_NAME, reqPhaseId);
			basicForward(request, response, "FollowupJobOrder");
		} else
			sendRedirect(request, response, "FollowupJobOrderSearch");

	}

	protected void setFinalActionDetails(JobOrder jobOrder, HttpServletRequest request) throws FileNotFoundException,
			IOException, ServletException {
		HospitalDevice device = jobOrder.getHospitalDevice();

		jobOrder.setFinalAction(null);
		jobOrder.setFinalActionDate(null);
		jobOrder.setFinalReportUrl(null);
		jobOrder.setFinalAgentReportUrl(null);

		jobOrder.setFinalAction(WebUtil.getParamValue(request, "finalAction", null));
		jobOrder.setFinalActionDate(WebUtil.getParamValueAsDate(request, "finalActionDate", null));

		if (!jobOrder.getFixIncludingSpareParts()
				|| (jobOrder.getFixIncludingSpareParts() && jobOrder.getSpSource() != Enums.JOBORDER_SP_SOURCES.AGENT
						.getId())) {
			jobOrder.setSecondAction(jobOrder.getFinalAction());
			jobOrder.setSecondActionDate(jobOrder.getFinalActionDate());
		}

		String[] finalReportUrl = FileUtil.uploadFiles(request, "finalReportUrl");
		// if (finalReportUrl != null && finalReportUrl.length != 0)
		jobOrder.setFinalReportUrl(Common.concatenateValues(finalReportUrl));

		if (jobOrder.getAgentMustAttend()) {
			String[] finalAgentReportUrl = FileUtil.uploadFiles(request, "finalAgentReportUrl");
			// if (finalAgentReportUrl != null && finalAgentReportUrl.length !=
			// 0)
			jobOrder.setFinalAgentReportUrl(Common.concatenateValues(finalAgentReportUrl));
		}

		if (WebUtil.userHasRightToCloseJobOrder(request) && request.getParameter("closed") != null) {
			jobOrder.setClosed(true);
			jobOrder.setClosedBy(getSessionUser(request));
			jobOrder.setClosedIn(new Date());
			jobOrder.setActualCloseDate(WebUtil.getParamValueAsDate(request, "actualCloseDate", null));
			jobOrder.setLastCompletedPhaseId(Enums.JOBORDER_FOLLOWUP_PHASES.FINAL_ACTION_TAKEN.getId());
			device.setStatus(Enums.DEVICE_STATUS.WORK_PROPERLY.getStatus());
		}
	}

	public boolean skipActionProcessing(HttpServletRequest request) {
		return WebUtil.getParamValue(request, Default.SAVE_THEN_GO_BACK_BTN_NAME, null) != null;

	}
}
